package com.gopay.scheduler.job;

import com.gopay.common.constants.fund.FundFileSourceConstants;
import com.gopay.common.domain.fund.vo.FileBizParam;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.FastJsonUtil;
import com.gopay.remote.fundcps.service.FileHandlerService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.fund.FundBaseOperateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 银行赎回文件落地
 *
 * @author baiyunpeng
 * @date 2016-04-26 13:36:47
 */
@Controller
public class FundRedeemFileJob extends GopayBaseJob {

    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(FundRedeemFileJob.class);

    @Resource(name = "fileHandlerService")
    private FileHandlerService fundFileRemoteService;
    @Autowired
    private FundBaseOperateService operateService;

    @Override
    protected void doJobInternal() {
        LOGGER.info("【赎回操作任务一】监管行提供给国付宝赎回分红退款文件（0文件）生成定时任务开始！=============" + DateUtils.format(new Date()), DateUtils.yyyy_MM_dd_HH_mm_ss);
        String settleDay = DateUtils.format(new Date(), DateUtils.FILEDATE_PATTERM);
        try {
            LOGGER.info("【赎回操作任务一】获取监管行文件rtfund_redeem_yyyyMMdd.txt 开始");
            List<String> custIds = operateService.pullFileFromSftp(settleDay);
            if (custIds == null || custIds.size() == 0) {
                LOGGER.info("【赎回操作任务一】获取监管行文件结束，今日没有需要处理的数据！");
            } else {
                LOGGER.info("【赎回操作任务一】获取监管行文件结束，今日有数据的基金有：{}，开始联机fundcps", FastJsonUtil.toJSONString(custIds));
                String res = fundFileRemoteService.superviseRedeemFile(new FileBizParam(settleDay, custIds, FundFileSourceConstants.CORN));
                LOGGER.info("【赎回操作任务一】联机fundcps结束，返回结果：{}", res);
            }
        } catch (Exception e) {
            LOGGER.error("【赎回操作任务一】获取银行数据异常！", e);
        }
        LOGGER.info("【赎回操作任务一】监管行提供给国付宝赎回分红退款文件（0文件）定时任务执行完毕！");
    }

}
